<?php

namespace app\common\service;

use app\model\site\SiteConfig;
use function miUnserialize;

class CommonSiteConfigService
{
    const UPLOAD = 'upload';    // 上传配置
    const WEB_SET = 'web_set';  // web端和h5端配置
    const WX_MP = 'wx_mp'; // 微信小程序配置
    const PAY_SET = 'pay_set'; // 支付配置
    const LOGIN = 'login';  // 注册登录配置
    const POINTS = 'points';    // 积分设置
    const STORE = 'store'; // 商城设置
    const SMS = 'sms'; // 短信设置
    const DIST = 'dist'; // 分销设置

    private static ?self $_instance = null;

    public static function instance(): static
    {
        if (!self::$_instance){
            self::$_instance = new self();
        }
        return self::$_instance;
    }


    public function getConfig(int $siteId, string $key=''): array
    {
        $info = SiteConfig::withoutField('create_time,update_time')->where('site_id', $siteId)->where('key', $key)->find();
        if($info){
            $info->value = miUnserialize($info->value);
            $info = $info->toArray();
        }else{
            $info = ($this->defaultConfig())[$key];
        }
        return $info;
    }

    public function getConfigValue(int $siteId, string $key='') {
        return $this->getConfig($siteId, $key)['value'];
    }

    /**
     * 微信支付配置
     * @param int $siteId
     * @return mixed
     */
    public function getWxPayConfig(int $siteId): mixed
    {
        $configPay = $this->getConfigValue($siteId, CommonSiteConfigService::PAY_SET);
        $configWxMp = $this->getConfigValue($siteId, CommonSiteConfigService::WX_MP);
        $config = $configPay['wx'];
        $config['appid'] = $configWxMp['appid'];
        return $config;
    }


    private function defaultConfig(){
        return [
            // 上传设置
            self::UPLOAD => [
                'key' => self::UPLOAD,
                'title' => '上传设置',
                'value' => [
                    'driver' => 'local',
                    'qiniu' => [
                        'bucket' => '',
                        'access_key' => '',
                        'secret_key' => '',
                        'domain' => 'http://'
                    ],
                    'ali' => [
                        'bucket' => '',
                        'access_id' => '',
                        'access_secret' => '',
                        'domain' => 'http://',
                        'endpoint' => '',
                    ],
                    'qcloud' => [
                        'bucket' => '',
                        'secret_id' => '',
                        'secret_key' => '',
                        'domain' => 'http://',
                        'region' => '',
                    ],
                    'file_size' => 0,
                    'file_ext' => '',
                    'image_size' => 0,
                    'image_ext' => '',
                    'video_size' => 0,
                    'video_ext' => '',
                ],
            ],
            // web端配置
            self::WEB_SET => [
                'key' => self::WEB_SET,
                'title' => 'pc和h5设置',
                'value' => [
                    'pc_show' => 1,     // 是否开启pc端 1-开启 2-关闭
                    'h5_show' => 1,     // 是否开启h5端 1-开启 2-关闭
                    'domain' => '',     // 站点域名
                ],
            ],
            // 微信小程序配置
            self::WX_MP => [
                'key' => self::WX_MP,
                'title' => '微信小程序',
                'value' => [
                    'appid' => '',     // 小程序的appid
                    'secret' => '',     // 小程序的secret
                ],
            ],
            // 支付配置
            self::PAY_SET => [
                'key' => self::PAY_SET,
                'title' => '支付配置',
                'value' => [
                    'wx' => [
                        'is_on' => 1,
                    ],
                    'ali' => [
                        'is_on' => 1,
                    ],
                    'balance' => [
                        'is_on' => 1,
                    ],
                ],
            ],
            // 登录配置
            self::LOGIN => [
                'key' => self::LOGIN,
                'title' => '登录配置',
                'value' => [
                    'is_force_bind_mobile' => 1,   // 强制绑定手机号
                    'oauth_login_type' => [],       // 第三方登录方式
                    'give_coupon_id' => 0,  // 注册赠送优惠券
                    'give_points' => 0, // 注册赠送积分
                ],
            ],
            // 积分设置
            self::POINTS => [
                'key' => self::POINTS,
                'title' => '积分配置',
                'value' => [
                    'is_on' => 1, // 是否开启积分功能
                    'is_on_dec' => 1, // 是否开启积分抵扣
                    'money_rate' => 1000,  // 积分与人民币的比例，多少积分可以抵扣一元
                    'give_points_rate' => 1 // 每消费1元，赠送的积分数量
                ]
            ],
            self::STORE => [
                'key' => self::STORE,
                'title' => '商城设置',
                'value' => [
                    'is_on_sign' => 1,  // 是否开启签到
                    'is_on_coupon' => 1,    // 是否开启优惠券功能
                    'order_cancel_delay' => 60*60*24,   // 未支付订单取消期限
                    'order_receive_delay' => 60*60*24*14,  // 订单自动确认收货期限
                    'order_close_delay' => 60*60*24*30,    // 确认收货订单关闭期限
                ],
            ],
            // 短信配置
            self::SMS => [
                'key' => self::SMS,
                'title' => '短信平台配置',
                'value' => [
                    'show_type' => 'ali', // 默认使用阿里云短信配置  ali-阿里云 qixin-企信
                    'ali' => [
                        'id' => '',     // 阿里云
                        'secret' => '',     // 阿里云
                        'sign' => '',   // 签名
                    ],
                    'qixin' => [
                        'url' => '',     // url地址
                        'uid' => '',     // 企业id
                        'account' => '',   // 账号
                        'password' => '',   // 密码
                    ],
                ],
            ],
            // 分销配置
            self::DIST => [
                'key' => self::DIST,
                'title' => '分销配置',
                'value' => [
                    'is_on' => 1,   // 是否开启分销
                    'dist_type' => 1, // 分销模式 1-人人分销
                    'one_reward_rate' => 10,   // 一级分销佣金比例
                    'two_reward_rate' => 2,  // 二级分销拥挤比例
                    'store_manager_price' => 10, // 成为店长付款金额（赠送的样品基金金额）
                    'store_manager_num' => 5,   // 每轮赠送样品基金的邀请数量
                    'store_manager_num_price' => 200,   // 邀请店长赠送的样品基金金额

                    'reward_give_type' => 1,    // 拥挤结算方式 1-确认收货结算 2-订单关闭结算
                ],
            ],

        ];
    }
}
